<?php

namespace Common\Model;

use Think\Model;

class RankModel extends Model
{
    protected $time = array();

    public function _initialize()
    {
        $time = array();
        // $time['day'][0]-本期 $time['day'][1]-上期
        $time['day'] = array(date('Y\dz'), date('Y\dz', mktime(null, null, null, date('n'), date('d') - 1)));
        $time['week'] = array(date('Y\wW'), date('Y\wW', mktime(null, null, null, date('n'), date('d') - 7)));
        $time['month'] = array(date('Y\mm'), date('Y\mm', mktime(null, null, null, date('n') - 1, 1)));
        $time['lastweek'] = array(
            date('Y\wW', mktime(null, null, null, date('n'), date('d') - 7)),
            date('Y\wW', mktime(null, null, null, date('n'), date('d') - 14))
        );

        $this->time = $time;
    }

    /**
     * 添加排行记录
     * @param string|int $type 1-助人(answer回答数) 2-好评(praise回答点赞数) 3-知识(optimal回答采纳数) 4-财富(gold新增积分数)
     * @param int        $uid
     * @param int        $num  指数值
     */
    public function add_record($type, $uid, $num = 1)
    {
        $type = $this->set_type($type);
        $map['type'] = $type;
        $map['uid'] = $uid;
        foreach ($this->time as $index => $time) {
            // 不需要记录数据的key
            if ($index == 'lastweek') continue;
            unset($data);
            $map['time'] = $time[0];
            $record = $this->where($map)->find();
            if (!$record) {
                $data['update_time'] = time();
                $data['time'] = $time[0];
                $data['num'] = $num;
                $data['type'] = $type;
                $data['uid'] = $uid;
                $map['time'] = $time[1];
                $data['refer'] = $this->where($map)->getField('num') ?: 0;
                $this->add($data);
            } else {
                $data['num'] = $record['num'] + $num;
                $data['update_time'] = time();
                $this->where($map)->setField($data);
            }
        }
    }

    /**
     * 获取排行
     * @param string|int $type  1-助人(回答数) 2-好评(回答点赞数) 3-知识(回答采纳数) 4-财富(新增积分数)
     * @param  string    $times 可选值[day week month]
     * @param int        $limit 获取条数
     * @return array
     */
    public function get_rank($type, $times, $limit = 10)
    {
        $type = $this->set_type($type);
        $map['type'] = $type;
        $ret = array();
        foreach ($this->time as $key => $time) {
            if ($key !== $times) continue;
            $map['time'] = $time[0];
            $order = array('num' => 'desc', 'update_time');
            $field = array('id', 'uid', 'num', 'refer');
            $list = $this->where($map)->order($order)->limit($limit)->field($field)->select();
        }
        $list = array_map(array($this, 'fill'), $list);
        return $list;
    }

    /**
     * 设置排行榜类型
     * @param string $type 1-助人(回答数) 2-好评(回答点赞数) 3-知识(回答采纳数) 4-财富(新增积分数)
     * @return int
     */
    protected function set_type($type)
    {
        if (is_string($type)) {
            $type_map = array(
                'answer'  => 1,
                'praise'  => 2,
                'optimal' => 3,
                'gold'    => 4,
            );
            $type = $type_map[$type];
        }
        return (int)$type;
    }

    /** 扩展字段
     * @param $item
     * @return mixed
     */
    protected function fill($item)
    {
        $item['chg'] = $item['num'] - $item['refer'];
        $item['user'] = D('User')->get_user_one($item['uid'], true, 1);
        return $item;
    }
}